package com.ecco.persistent;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class AccountAgentBean implements AccountAgentRemote, AccountAgentLocal {

	//	 property constants
	public static final String ACC_NAME = "acc_name";
	public static final String CONTACT = "contact";
	public static final String PHONE = "phone";
	public static final String EMAIL = "email";
	public static final String DATE = "date";
	
	@PersistenceContext(unitName="eccodb") private EntityManager entityManager;
	
	public void save(Account persistentInstance) {
		entityManager.persist(persistentInstance);
	}
	
	public void delete(Account persistentInstance) {
		entityManager.remove(persistentInstance);
	}
	
	public Account update(Account detachedInstance) {
		return entityManager.merge(detachedInstance);
	}

	
	@SuppressWarnings("unchecked")
	public List<Account> findByAccName(String acc_name) {
		String queryString = "SELECT model FROM Account model WHERE model.acc_name = :accNameValue";
		return entityManager.createQuery(queryString).setParameter(
				"accNameValue", acc_name).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Account> searchByAccName(String acc_name) {
		String queryString = "SELECT model FROM Account model WHERE model.acc_name LIKE :accNameValue";
		return entityManager.createQuery(queryString).setParameter(
				"accNameValue", acc_name).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Account> findByContact(String contact) {
		String queryString = "SELECT model FROM Account model WHERE model.contact = :accContactValue";
		return entityManager.createQuery(queryString).setParameter(
				"accContactValue", contact).getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Account> findByPhone(String phone) {
		String queryString = "SELECT model FROM Account model WHERE model.phone = :accPhoneValue";
		return entityManager.createQuery(queryString).setParameter(
				"accPhoneValue", phone).getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Account> findByEmail(String email) {
		String queryString = "SELECT model FROM Account model WHERE model.email = :accEmailValue";
		return entityManager.createQuery(queryString).setParameter(
				"accEmailValue", email).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<Account> findByDate(Date date) {
		String queryString = "SELECT model FROM Account model WHERE model.date = :accDateValue";
		return entityManager.createQuery(queryString).setParameter(
				"accDateValue", date).getResultList();
	}
	
	public Account findAccountById(Integer acc_id) {
		Account account = entityManager.find(Account.class, acc_id);
		account.getAocRecords().size();
		return account;
	}
	
	@SuppressWarnings("unchecked")
	public List<Account> findAll() {
		String queryString = "SELECT model FROM Account model";
		return entityManager.createQuery(queryString).getResultList();
	}
	
	public void addAccount(String acc_name, String contact, String phone, String email, Date date) {
		Account account = new Account();
		account.setAcc_name(acc_name);
		account.setContact(contact);
		account.setPhone(phone);
		account.setEmail(email);
		account.setDate(date);
		entityManager.persist(account);
	}
	
	public void updateAccount(Integer acc_id, String acc_name, String contact, String phone, String email, Date date) {
		Account account = entityManager.find(Account.class, acc_id);
		if (account!=null) {
			account.setAcc_name(acc_name);
			account.setContact(contact);
			account.setPhone(phone);
			account.setEmail(email);
			account.setDate(date);
			entityManager.merge(account);
		}
	}
		
	public void removeAccountById(Integer acc_id) {
		Account account = entityManager.find(Account.class, acc_id);
		if (account!=null) entityManager.remove(account);
	}
	
	public Integer findNumOfAOCRecordsByAcc_id(Integer acc_id) {
		Account account = entityManager.find(Account.class, acc_id);
		return account.getAocRecords().size();
	}
	
	@SuppressWarnings("unchecked")
	public Boolean checkByAccName(String acc_name) {
		String queryString = "SELECT model.acc_id FROM Account model WHERE model.acc_name = :accNameValue";
		List checkAccountResult =  entityManager.createQuery(queryString).setParameter(
				"accNameValue", acc_name).getResultList();
		if (checkAccountResult.size()== 0){return false;}
			return true;
	}
	
	@SuppressWarnings("unchecked")
	public List<String> searchByAccNameRetrunAccName(String acc_name) {
		String queryString = "SELECT model.acc_name FROM Account model WHERE model.acc_name LIKE :accNameValue";
		return entityManager.createQuery(queryString).setParameter(
				"accNameValue", acc_name).getResultList();
	}
	
}
